import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

// Future<SharedPreferences> _incrementCounter() async {
//   await SharedPreferences.getInstance();
// }

class LocalStorage {
  static Future<void> setstr(String key, String value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString(key, value);
  }

  static Future<void> setJson(String key, Map<String, dynamic> value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setString(key, json.encode(value));
  }

 static Future<Map<String, dynamic>> getJson(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? jsonString = prefs.getString(key);
    if (jsonString != null) {
      return json.decode(jsonString);
    }
    return {};
  }

  static Future<String?> getstr(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getString(key).toString();
  }

  static Future<void> clearstr(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.remove(key);
  }
  /// 资讯分类-存储
  static Future<List<Map<String, dynamic>>> _getNewsTypesList() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? newsTypesString = prefs.getString('newsTypes');
    if (newsTypesString != null) {
      List<Map<String, dynamic>> newsTypes =
          (json.decode(newsTypesString) as List)
              .map((item) => Map<String, dynamic>.from(item))
              .toList();
      return newsTypes;
    }
    return [];
  }

  /// 资讯分类-存储
  static Future<List<Map<String, String>>> getNewsTypes() async {
    List<Map<String, dynamic>> newsTypes = await _getNewsTypesList();
    return newsTypes.map((item) => item.map((key, value) => MapEntry(key, value.toString()))).toList();
  }

  Future<void> deleteNewsType(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List<Map<String, dynamic>> newsTypes = await _getNewsTypesList();
    newsTypes.removeWhere((item) => item['key'] == key);
    await prefs.setString('newsTypes', json.encode(newsTypes));
  }

  static Future<void> updateNewsType(String key, Map<String, String> newValue) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List<Map<String, dynamic>> newsTypes = await _getNewsTypesList();
    for (var item in newsTypes) {
      if (item['key'] == key) {
        item.addAll(newValue);
        break;
      }
    }
    await prefs.setString('newsTypes', json.encode(newsTypes));
  }

  /// 新增一个插入 
  static Future<void> insertNewsType(List<Map<String, String>> newItems) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    List<Map<String, dynamic>> newsTypes = await _getNewsTypesList();
    newsTypes.addAll(newItems);
    await prefs.setString('newsTypes', json.encode(newsTypes));
  }
}
